package com.cjzuji.rental.controller.order;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjzuji.rental.Bean.YoupinRequest;
import com.cjzuji.rental.dao.OrderContractDao;
import com.cjzuji.rental.dao.OrderBillDao;
import com.cjzuji.rental.dao.OrderDao;
import com.cjzuji.rental.dao.OrderUserDao;
import com.cjzuji.rental.mapper.OrderMapper;
import com.cjzuji.rental.mapper.RequestMapper;
import com.cjzuji.rental.pojo.Orders;
import com.cjzuji.rental.pojo.RequestHeard;
import com.cjzuji.rental.service.YouPinService;
import com.cjzuji.rental.util.Tool;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author bag
 * @version V1.0
 * @date 2025/7/7 14:53
 * @since 1.0
 */

@RestController
@RequestMapping("/order/youping")
@Api(tags = "优品订单列表")
//@Component
public class YouPinController {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    RequestMapper requestMapper;
    @Autowired
    YouPinService youPinService;

    private static final Logger logger = LoggerFactory.getLogger(YouPinController.class);
    //http://127.0.0.1:9099/order/youping/getListInfo
    //获取所有数据
    @GetMapping("/getListInfo")
    @ApiOperation(value = "获取所有订单列表详情", notes = "优品所有订单列表和订单详情")
    //@Scheduled(fixedRate = 60 * 60 * 2000)
    public List<Map<String, Object>> getOrderList(){
        //从数据库获取
        try{
            QueryWrapper<RequestHeard> queryWrapper = new QueryWrapper<>();
            RequestHeard requestHeard = requestMapper.selectOne(queryWrapper);
            String accesstoken = requestHeard.getAccesstoken();
            String authorization = requestHeard.getAuthorization();
            List<Map<String, Object>> result = youPinService.getOrderssList(accesstoken, authorization);
            return result;
        }catch (Exception e){
            logger.error("获取 优品 所有订单列表详情失败");
            return null;
        }
    }

    //获取租用中的数据
    @PutMapping("/upDateBill")
    @ApiOperation(value = "获取租用中的数据", notes = "租用中的数据")
    //@Scheduled(fixedRate = 60 * 60 * 2000)
    public List<Map<String, Object>> upDateBill(){
        //从数据库获取
        try{
            QueryWrapper<RequestHeard> queryWrapper = new QueryWrapper<>();
            RequestHeard requestHeard = requestMapper.selectOne(queryWrapper);
            String accesstoken = requestHeard.getAccesstoken();
            String authorization = requestHeard.getAuthorization();
            List<Map<String, Object>> result = youPinService.upDateBillList(accesstoken, authorization, "useList");
            return result;
        }catch (Exception e){
            logger.error("获取 优品 正在租用的订单列表详情失败");
            return null;
        }
    }

//http://127.0.0.1:9099/order/youping/getRequest
    //更新请求头
    @PutMapping("/updataToken")
    @Transactional
    public int getOrderList(@RequestParam("accesstoken") String accesstoken, @RequestParam("authorization") String authorization,
                            @RequestParam("azjtk") String azjtk, @RequestParam("rrzauthorization") String rrzauthorization,
                            @RequestParam("qlypaccesstoken") String qlypaccesstoken, @RequestParam("qlypauthorization") String qlypauthorization,
                            @RequestParam("cjazjtk") String cjazjtk, @RequestParam("linglcookie") String linglcookie,
                            @RequestParam("lingltoken") String lingltoken, @RequestParam("aidaccesstoken") String aidaccesstoken,
                            @RequestParam("gclinglcookie") String gclinglcookie, @RequestParam("gclingltoken") String gclingltoken,
                            @RequestParam("ersqlypaccesstoken") String ersqlypaccesstoken,@RequestParam("ersqlypauthorization") String ersqlypauthorization,
                            @RequestParam("ersrrzauthorization") String ersrrzauthorization,@RequestParam("ersrrzcookie") String ersrrzcookie){
        //将参数保存到数据库
        RequestHeard requesth = new RequestHeard();
        Long id = 1L;
        requesth.setId(id);
        requesth.setAccesstoken(accesstoken);
        requesth.setAuthorization(authorization);
        requesth.setAzjtk(azjtk);
        requesth.setRrzauthorization(rrzauthorization);
        requesth.setQlypaccesstoken(qlypaccesstoken);
        requesth.setQlypauthorization(qlypauthorization);
        requesth.setCjazjtk(cjazjtk);
        requesth.setLinglcookie(linglcookie);
        requesth.setLingltoken(lingltoken);
        requesth.setAidaccesstoken(aidaccesstoken);
        requesth.setGclinglcookie(gclinglcookie);
        requesth.setGclingltoken(gclingltoken);
        requesth.setErsqlypaccesstoken(ersqlypaccesstoken);
        requesth.setErsqlypauthorization(ersqlypauthorization);
        requesth.setErsrrzauthorization(ersrrzauthorization);
        requesth.setErsrrzcookie(ersrrzcookie);
        int result = requestMapper.updateById(requesth);
        return result;
    }

    //从数据库中获取
    @GetMapping("/getToken")
    @Transactional
    public List<RequestHeard> getToken(){
        List<RequestHeard> result = requestMapper.selectList(null);
        return result;
    }
    //风控审批接口
    @PostMapping("/riskApproval")
    @ApiOperation(value = "风控审批", notes = "风控审批")
    public String riskApproval(@RequestBody Orders orders, @RequestParam String orderId) {
        return youPinService.riskApproval(orders, orderId);
    }
    //查询风控结果接口
    @PostMapping("/queryApproval")
    @ApiOperation(value = "查询风控结果", notes = "查询风控结果")
    public String queryApproval(@RequestParam("creditId") String creditId) {
        return youPinService.queryApproval(creditId);
    }
}
